package com.zretc.support.service.serviceimpl;

import com.zretc.support.dao.OnlineDao;
import com.zretc.support.entity.Online;
import com.zretc.support.service.OnlineService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class OnlineServiceImpl implements OnlineService {
    @Autowired
    OnlineDao od;

    @Override
    public Page<Online> selectByPage(String keywords, String senderId, String receiverId, Integer page, Integer limit) {
        Pageable pageable= PageRequest.of(page-1,limit);
        Page<Online> pd=od.selectByPage(keywords,senderId,receiverId,pageable);
        return pd;
    }

    @Override
    public Page<Online> selectByDate(Date time1, Date time2, String senderId, String receiverId, Integer page, Integer limit) {
        Pageable pageable= PageRequest.of(page-1,limit);
        Page<Online> pd=od.selectByDate(time1,time2,senderId,receiverId,pageable);
        return pd;
    }

    @Override
    public int addOnline(Online online) {
        Online online1=od.save(online);
        if (online1!=null){
            return 1;
        }
        return 0;
    }

    @Override
    public int updOnline(Online online) {
        od.save(online);
        return 1;
    }

    @Override
    public int delOnlById(Integer onlineId) {
        try{
            od.deleteById(onlineId);
        }catch (Exception e){
            return 0;
        }
        return 1;
    }
}
